﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Snowflake.Common
{
    /// <summary>
    /// Represents indexed collection of keys and values
    /// </summary>
    /// <typeparam name="TKey">The type of the keys in the dictionary</typeparam>
    /// <typeparam name="TValue">The type of the values in the dictionary</typeparam>
    [Serializable]
    public class IndexedDictionary<TKey, TValue>
    {
        /// <summary>
        /// Dictionary that stores all keys and values
        /// </summary>
        private Dictionary<TKey, TValue> _Dictionary;

        /// <summary>
        /// Gets or sets the value associated with the specified key
        /// </summary>
        /// <param name="property">The key of the value to get or set</param>
        /// <returns>The value for specified key</returns>
        public TValue this[TKey property]
        {
            get
            {
                if (this._Dictionary == null)
                    this._Dictionary = new Dictionary<TKey, TValue>();

                if (this._Dictionary.ContainsKey(property))
                    return this._Dictionary[property];
                else
                    return default(TValue);
            }
            set
            {
                if (this._Dictionary == null)
                    this._Dictionary = new Dictionary<TKey, TValue>();

                if (this._Dictionary.ContainsKey(property))
                {
                    if (value != null)
                        this._Dictionary[property] = value;
                    else
                        this._Dictionary.Remove(property);
                }
                else
                {
                    if (value != null)
                        this._Dictionary.Add(property, value);
                }
            }
        }
    }
}
